/**
 * Created by shidaiapp on 2017/9/29.
 */

/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;
  tiyData.tStatus.modalEditCompany.isShow = false;
  tiyData.tParams.name = "";
 };

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOther.getDeliveryCompanyList({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "name": tiyData.tParams.name
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.companyList.splice(0, tiyData.tData.companyList.length);
      let companyPage = response.result;
      tiyData.tPage.total = companyPage.total;
      tiyData.tPage.stepNumber = companyPage.pageSize;
      companyPage.list.forEach(
        function (item) {
          tiyData.tData.companyList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};


/**
 * 提交快递公司信息
 * @param self
 * @param tiyData
 */
export const submitCompanyInfo = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOther.saveDeliveryCompany(tiyData.tParams.companyInfo).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success('保存成功!');
      webList(self, tiyData);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 删除快递公司信息
 * @param self
 * @param tiyData
 */
export const deleteCompany = (self, tiyData, id, name) => {
  self.$Spin.show();
  self.$apiOther.deleteDeliveryCompany({"id":id, "name":name}).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success('删除成功!');
      webList(self, tiyData);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
